﻿namespace SupportSystemUI.Controls.Basic
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Effects;

    /// <summary>
    /// Interaction logic for CategoryButton.xaml
    /// </summary>
    public partial class CategoryButton : UserControl
    {
        /// <summary>
        /// The dependency property for the name of the icon presented on the button.
        /// </summary>
        public static readonly DependencyProperty IconNameProperty = DependencyProperty.Register(
            "IconName", typeof(string), typeof(CategoryButton), new UIPropertyMetadata(string.Empty));

        /// <summary>
        /// The dependency property for the glowing property of the button.
        /// </summary>
        public static readonly DependencyProperty IsGlowingProperty = DependencyProperty.Register(
            "IsGlowing", typeof(bool), typeof(CategoryButton), new UIPropertyMetadata(false));
        
        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryButton"/> class.
        /// </summary>
        public CategoryButton()
        {
            this.InitializeComponent();

            this.MouseEnter += (sender, args) =>
            {
                Color oldColor = (this.colorBorder.Background as SolidColorBrush).Color;
                this.colorBorder.Background = new SolidColorBrush(Color.Add(oldColor, Color.FromRgb(100, 100, 100)));
            };

            this.MouseLeave += (sender, args) =>
            {
                Color oldColor = (this.colorBorder.Background as SolidColorBrush).Color;
                this.colorBorder.Background = new SolidColorBrush(Color.Subtract(oldColor, Color.FromRgb(100, 100, 100)));
            };
        }

        /// <summary>
        /// Gets or sets the dependency property for the name of the icon presented on the button.
        /// </summary>
        public string IconName
        {
            get { return (string)this.GetValue(IconNameProperty); }
            set { this.SetValue(IconNameProperty, value); }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this button is glowing or not.
        /// </summary>
        public bool IsGlowing
        {
            get { return (bool)this.GetValue(IsGlowingProperty); }
            set { this.SetValue(IsGlowingProperty, value); }
        }

        /// <summary>
        /// Associates a certain action with this button's click event.
        /// </summary>
        /// <param name="action">Action to be executed.</param>
        public void AddClickAction(Action action)
        {
            this.mainRectangle.MouseUp += (sender, args) => { action(); };
        }

        /// <summary>
        /// Overrides the actual OnPropertyChanged method of class UserControl.
        /// </summary>
        /// <param name="e">Dependency property changed event arguments.</param>
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (object.ReferenceEquals(e.Property, IconNameProperty))
            {
                if (string.IsNullOrWhiteSpace(e.NewValue as string) == false)
                {
                    this.iconRectangle.Fill =
                        Application.Current.Resources[e.NewValue] as VisualBrush;
                }
            }
            else if (object.ReferenceEquals(e.Property, IsGlowingProperty))
            {
                Canvas canvas = (this.iconRectangle.Fill as VisualBrush).Visual as Canvas;
                if ((bool)e.NewValue == true)
                {
                    canvas.Effect = new DropShadowEffect
                    {
                        ShadowDepth = 8,
                        Direction = 200,
                        Color = Color.FromRgb(181, 48, 39),
                        Opacity = 1
                    };
                }
                else
                {
                    canvas.Effect = null;
                }
            }
        }
    }
}
